﻿using System;
using System.Linq;
using Lbb.Data;
using Lbb.Web.Mvc;
using Lbb.URating.DL;
using Lbb.Common;

namespace Lbb.URating.Models
{
    /// <summary>
    /// 评分项数据列表
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickRatingDataList : ExtDataList<U_ClickRating>
    {
        #region Ctors
        /// <summary>
        /// 构建评分项数据列表
        /// </summary>
        public U_ClickRatingDataList()
        {
            if (Request != null)
            {
                CategoryIdsStr = Request["categoryIds"];
                ClickIdsStr = Request["clickIds"];
                ObjtypeIdsStr = Request["objIds"];
                MinClickCount = GetReqInt("minClickCount");
                MaxClickCount = GetReqInt("maxClickCount");
                MinAverageRating = GetReqDecimal("minAverageRating");
                MaxAverageRating = GetReqDecimal("maxAverageRating");
            }
        }
        #endregion Ctors

        #region Props

        #region CategoryIds
        /// <summary>
        /// 所属分类
        /// </summary>
        public string CategoryIdsStr
        {
            get
            {
                return RouteValues.categoryIds as string;
            }
            set
            {
                RouteValues.categoryIds = value;
                _categoryIdsChanged = true;
            }
        }
        private bool _categoryIdsChanged = false;
        /// <summary>
        /// 所属分类
        /// </summary>
        public Guid[] CategoryIds
        {
            get
            {
                if (_categoryIds == null || _categoryIdsChanged)
                {
                    _categoryIds = CategoryIdsStr.ToGuids(removeEmpty: true);
                    _categoryIdsChanged = false;
                }
                return _categoryIds;
            }
            set
            {
                CategoryIdsStr = value.Merger();
                _categoryIdsChanged = false;
            }
        }
        private Guid[] _categoryIds;
        #endregion CategoryIds

        #region ClickIds
        /// <summary>
        /// 点击器
        /// </summary>
        public string ClickIdsStr
        {
            get
            {
                return RouteValues.clickIds as string;
            }
            set
            {
                RouteValues.clickIds = value;
                _clickIdsChanged = true;
            }
        }
        private bool _clickIdsChanged = false;
        /// <summary>
        /// 点击器
        /// </summary>
        public Guid[] ClickIds
        {
            get
            {
                if (_clickIds == null || _clickIdsChanged)
                {
                    _clickIds = ClickIdsStr.ToGuids(removeEmpty: true);
                    _clickIdsChanged = false;
                }
                return _clickIds;
            }
            set
            {
                ClickIdsStr = value.Merger();
                _clickIdsChanged = false;
            }
        }
        private Guid[] _clickIds;
        #endregion ClickIds

        #region ObjtypeIds
        /// <summary>
        /// 关联对象
        /// </summary>
        public string ObjtypeIdsStr
        {
            get
            {
                return RouteValues.objIds as string;
            }
            set
            {
                RouteValues.objIds = value;
                _objIdsChanged = true;
            }
        }
        private bool _objIdsChanged = false;
        /// <summary>
        /// 关联对象
        /// </summary>
        public string[] ObjtypeIds
        {
            get
            {
                if (_objIds == null || _objIdsChanged)
                {
                    _objIds = ObjtypeIdsStr.Split(removeEmpty: true);
                    _objIdsChanged = false;
                }
                return _objIds;
            }
            set
            {
                ObjtypeIdsStr = value.Merger();
                _objIds = value;
                _objIdsChanged = false;
            }
        }
        private string[] _objIds;
        #endregion ObjtypeIds
        /// <summary>
        /// 点击量下限
        /// </summary>
        public int? MinClickCount
        {
            get
            {
                return RouteValues.minClickCount as int?;
            }
            set
            {
                RouteValues.minClickCount = value;
            }
        }
        /// <summary>
        /// 点击量上限
        /// </summary>
        public int? MaxClickCount
        {
            get
            {
                return RouteValues.maxClickCount as int?;
            }
            set
            {
                RouteValues.maxClickCount = value;
            }
        }
        /// <summary>
        /// 平均分下限
        /// </summary>
        public decimal? MinAverageRating
        {
            get
            {
                return RouteValues.minAverageRating as decimal?;
            }
            set
            {
                RouteValues.minAverageRating = value;
            }
        }
        /// <summary>
        /// 平均分上限
        /// </summary>
        public decimal? MaxAverageRating
        {
            get
            {
                return RouteValues.maxAverageRating as decimal?;
            }
            set
            {
                RouteValues.maxAverageRating = value;
            }
        }
        #endregion Props

        #region Methods
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="data">数据源</param>
        public void FillData(IQueryable<U_ClickRating> data)
        {
            if (data == null) throw new ArgumentNullException("data");

            switch (OrderBy)
            {
                case "clickCount_up":
                    data = data.OrderBy(d => d.ClickCount);
                    break;
                case "clickCount_down":
                    data = data.OrderByDescending(d => d.ClickCount);
                    break;
                case "averageRating_up":
                    data = data.OrderBy(d => d.AverageRating);
                    break;
                case "averageRating_down":
                    data = data.OrderByDescending(d => d.AverageRating);
                    break;
                case "title_up":
                    data = data.OrderBy(d => d.U_Click.Title);
                    break;
                case "title_down":
                    data = data.OrderByDescending(d => d.U_Click.Title);
                    break;
                case "isRating_up":
                    data = data.OrderBy(d => d.U_Click.IsRating);
                    break;
                case "isRating_down":
                    data = data.OrderByDescending(d => d.U_Click.IsRating);
                    break;
                case "minRating_up":
                    data = data.OrderBy(d => d.U_Click.MinRating);
                    break;
                case "minRating_down":
                    data = data.OrderByDescending(d => d.U_Click.MinRating);
                    break;
                case "maxRating_up":
                    data = data.OrderBy(d => d.U_Click.MaxRating);
                    break;
                case "maxRating_down":
                    data = data.OrderByDescending(d => d.U_Click.MaxRating);
                    break;
            }

            Items = data.GetPartialData(FromIndex, PageSize, out Total);
        }
        #endregion Methods
    }
}
